﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Steamrunner
{
    class SpriteSheetManager : GameComponent
    {
        Dictionary<string, SpriteSheet> spriteSheets = new Dictionary<string, SpriteSheet>();

        public SpriteSheetManager(Game game)
            : base(game)
        {
        }

        public void AddSpriteSheet(string id, SpriteSheet spriteSheet)
        {
            spriteSheets.Add(id, spriteSheet);
        }

        public void RemoveSpriteSheet(string id)
        {
            SpriteSheet spriteSheet = spriteSheets[id];
            if (spriteSheet != null)
            {
                spriteSheet.Dispose();
                spriteSheets.Remove(id);
            }
        }

        public SpriteSheet GetSpriteSheet(string id)
        {
            if (spriteSheets.ContainsKey(id))
            {
                return spriteSheets[id];
            }

            return null;
        }

        public void UpdateSpriteSheet(string id, GameTime gameTime)
        {
            if (spriteSheets.ContainsKey(id))
            {
                spriteSheets[id].Update(gameTime);
            }
        }

        public void DrawSpriteSheet(string id, GameTime gameTime, Vector2 position, float rotation)
        {
            if (spriteSheets.ContainsKey(id))
            {
                spriteSheets[id].Position = position;
                spriteSheets[id].Rotation = rotation;
                spriteSheets[id].Draw(gameTime);
            }
        }

        public void FlushSpriteSheet(string id)
        {
            spriteSheets[id].CurrentFrame = Point.Zero;
        }
    }
}
